library(dplyr)
library(data.table)

home.dir <- "path-to-data-appendix/3_Analysis_of_TAQ_data"
data.dir <- file.path(home.dir, 'WRDS_Server/Output')
table.dir <- file.path(home.dir, 'Final_Output/Tables')

getAbbr <- function(x){
  name <- "NA"
  if(x == "A") name <- "AMEX"
  if(x == "B") name <- "BX"
  if(x == "N") name <- "NYSE"
  if(x == "P") name <- "ARCA"
  if(x == "C") name <- "NSX"
  if(x == "T") name <- "NSDQ"
  if(x == "D") name <- "FINRA"
  if(x == "X") name <- "PSX"
  if(x == "M") name <- "CHX"
  if(x == "Z") name <- "BZX"
  if(x == "Y") name <- "BYX"
  if(x == "K") name <- "EDGX"
  if(x == "J") name <- "EDGA"
  if(x == "W") name <- "CBOE"
  if(x == "V") name <- "IEX"
  if(x == "I") name <- "ISE"
  return(name)
}
# Vectorize the abbreviation function
getAbbr <- Vectorize(getAbbr)

all_volume_2018 <- data.table::fread(file.path(data.dir, "All_Volume_2018.csv"))

colnames(all_volume_2018)

all_volume_2018$exchange <- getAbbr(all_volume_2018$EX)

ex_all_volume_2018 <- all_volume_2018 %>%
    group_by(exchange) %>%
    summarise(AllVolume_S  = sum(AllVolume_S, na.rm = TRUE),
              AllVolume_D  = sum(AllVolume_D, na.rm = TRUE),
              OnExVolume_S = sum(OnExVolume_S, na.rm = TRUE),
              OnExVolume_D = sum(OnExVolume_D, na.rm = TRUE),
              FINRAVolume_S  = sum(FINRAVolume_S, na.rm = TRUE),
              FINRAVolume_D  = sum(FINRAVolume_D, na.rm = TRUE),
              AuctionVolume_OnEx_S  = sum(AuctionVolume_S, na.rm = TRUE),
              AuctionVolume_OnEx_D  = sum(AuctionVolume_D, na.rm = TRUE),
              RegHoursVolume_OnEx_S = sum(RegHoursVolume_S, na.rm = TRUE),
              RegHoursVolume_OnEx_D = sum(RegHoursVolume_D, na.rm = TRUE))

write.csv(ex_all_volume_2018, file.path(table.dir, "ex_all_volume_2018.csv"))